package cn.tedu.store.admin.webapi.controller;

import cn.tedu.store.commons.pojo.admin.dto.PermissionAddNewDTO;
import cn.tedu.store.commons.pojo.admin.vo.PermissionListItemVO;
import cn.tedu.store.commons.web.JsonResult;
import cn.tedu.store.admin.service.IPermissionService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/permission")
@Validated
@Api(tags = "3.权限管理模块")
public class PermissionController {

    @Autowired
    IPermissionService permissionService;

    @PostMapping("/addNew")
    @ApiOperationSupport(order = 100)
    @ApiOperation("添加权限")
    public JsonResult addNew(@Validated PermissionAddNewDTO permissionAddNewDTO){
        permissionService.addNew(permissionAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除权限")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id){
        permissionService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改权限详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult updateById(@PathVariable Long id, PermissionAddNewDTO permissionAddNewDTO){
        log.debug("即将处理修改权限业务,参数id: {}, 权限信息: {}", id, permissionAddNewDTO);
        permissionService.updateById(id, permissionAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询权限列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<PermissionListItemVO>> list(){
        log.debug("开始处理获取权限列表的请求 参数:无");
        List<PermissionListItemVO> list = permissionService.list();
        log.debug("查询完成");
        return JsonResult.ok(list);
    }

}
